/* 通过mutations间接更新state的多个方法,能和后台交互的异步action
获取到数据（将已有数据） */
import {
  // RECEIVE_STU_WECHATId,
  RECEIVE_REPORT_CONTENT,
  RECEIVE_REPORT_CONTENT_PROF,
  RECEIVE_REPO_SUMMARY_PIE,
  RECEIVE_GROUPS,
  RECEIVE_PROF_INFO,
  RECEIVE_STUDENTS,
  RECEIVE_REPO_GROUP_LIST,
  RECEIVE_GROUP_OF_STU,
  RECEIVE_ALL_GROUPS,
  RECEIVE_GROUP_MEMBER,
  RECEIVE_STU_LIST_DIMLOOK,
  RECEIVE_GROUP_LIST_DIMLOOK
} from './mutation-types'
import state from './state'
import common from 'utilMethods/common'

/* 定义action
   同步：已有数据，存入state；
   异步：先从后台获取，再存入state
  */
export default{
  // 日报部分
  // 获取学生微信号
  // getStuWechatId (context) {
  //   context.commit('RECEIVE_STU_WECHATId')
  // },

  // 同步获取日报(学生)
  getReportContent ({commit}, reportContent) {
    reportContent.forEach((element) => {
      if (element.insertTime === null) {
        element.insertTime = element.date + 'T' + ' '
      } 
      element.insertTime = common.transferTimeString(element.insertTime).split('T')
    })
    commit(RECEIVE_REPORT_CONTENT, {reportContent}) // 存到state里面
  },

  // 同步获取日报（导师）
  getReportContentProf ({commit}, reportContentProf) {
    if (reportContentProf.length) {
      reportContentProf.forEach((element) => {
        if (element.insertTime === null) {
          element.insertTime = element.date + 'T' + ' ';
        } 
        element.insertTime = common.transferTimeString(element.insertTime).split('T');
        let timeCostOutput = 0;
        let timeCostStudy = 0;
        element.activities.forEach((activity) => {
          let start, end;
          if (activity.endTime !== null) end = Number(activity.endTime.slice(0, 2)) + Number(activity.endTime.slice(3, 5))/60;
          if (activity.startTime !== null) start = Number(activity.startTime.slice(0, 2)) + Number(activity.startTime.slice(3, 5))/60;
          let timeCost = ((end - start) + '').slice(0, 3);
          activity['timeCost'] = timeCost;
          // -2：休息；-1：个人疫情汇报；0：学习
          if (activity.type !== -2 && activity.type !== -1) {
            if (activity.typeContent.indexOf("学习") !== -1 ||
                activity.typeContent.indexOf("课") !== -1 ||
                activity.typeContent.indexOf("组会") !== -1) {
              timeCostStudy += Number(timeCost);
            } else {
              timeCostOutput += Number(timeCost);
            }
          }
        });
        element['timeCostOutput'] = (timeCostOutput + '').slice(0, 4);
        element['timeCostStudy'] = (timeCostStudy + '').slice(0, 4);
      })
      commit(RECEIVE_REPORT_CONTENT_PROF, {reportContentProf}) // 存到state里面
    }
  },


  // 同步获取饼状图的汇总数据,state里面没有start，end，所以在组件里面去发ajax请求
  getRepoSummaryPie ({commit}, repoSummaryPie) {
    commit(RECEIVE_REPO_SUMMARY_PIE, {repoSummaryPie}) // 存到state里面
  },

  // 同步所有分组信息
  getGroups ({commit}, groups) {
    commit(RECEIVE_GROUPS, {groups})
  },

  // 同步获取老师注册信息
  recordProfInfo ({commit}, profInfo) {
    commit(RECEIVE_PROF_INFO, {profInfo})
  },

  // 同步获取学生列表

  getStudents_1 ({commit}, students) {
    commit(RECEIVE_STUDENTS, {students})
  },

  // 同步获取组内成员的日报列表
  getRepoList ({commit}, repoGroupList) {
    commit(RECEIVE_REPO_GROUP_LIST, {repoGroupList})
  },

  // 同步获取学生所在分组
  getGroupsOfStu_1 ({commit}, groupsOfStu) {
    commit(RECEIVE_GROUP_OF_STU, {groupsOfStu}) // 存到state里面
  },

  // 同步获取分组的成员列表
  getGroupMember_1 ({commit}, groupMember) {
    commit(RECEIVE_GROUP_MEMBER, {groupMember}) // 存到state里面
  },

  // 同步获取 模糊查询学生的结果列表
  getStuListDimlook ({commit}, stuListDimlook) {
    commit(RECEIVE_STU_LIST_DIMLOOK, {stuListDimlook}) // 存到state里面
  },

  // 同步获取 模糊查询分组的结果列表
  getGroupListDimlook ({commit}, groupListDimlook) {
    commit(RECEIVE_GROUP_LIST_DIMLOOK, {groupListDimlook}) // 存到state里面
  },

  

}
